package com.woniu04;

import java.util.HashSet;

class Person {
    String name;
    int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public int hashCode() {
        // 通常基于对象的所有字段生成哈希码
        return name.hashCode() + Integer.valueOf(age).hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) return true; // 快速检查引用是否相同
        if (obj instanceof Person) {
            Person other = (Person) obj;
            // 比较所有相关的字段
            return this.name.equals(other.name) && this.age == other.age;
        }
        return false;
    }

    @Override
    public String toString() {
        return "Person{name='" + name + "', age=" + age + '}';
    }
}

public class Main {
    public static void main(String[] args) {
        HashSet<Person> people = new HashSet<>();
        people.add(new Person("Alice", 30));
        people.add(new Person("Bob", 25));
        people.add(new Person("Alice", 30)); // 试图添加重复元素

        for (Person person : people) {
            System.out.println(person);
        }
    }
}